package com.java110.mall.shop.bmo.shopCommunity.impl;

import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.shop.ShopCommunityDto;
import com.java110.intf.shop.IShopCommunityInnerServiceSMO;
import com.java110.intf.shop.IShopGoodsInnerServiceSMO;
import com.java110.po.shop.ShopCommunityPo;
import com.java110.po.shop.ShopGoodsPo;
import com.java110.mall.shop.bmo.shopCommunity.ISaveShopCommunityBMO;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service("saveShopCommunityBMOImpl")
public class SaveShopCommunityBMOImpl implements ISaveShopCommunityBMO {

    @Autowired
    private IShopCommunityInnerServiceSMO shopCommunityInnerServiceSMOImpl;

    @Autowired
    private IShopGoodsInnerServiceSMO shopGoodsInnerServiceSMOImpl;
    /**
     * 添加小区信息
     *
     * @param shopCommunityPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(ShopCommunityPo shopCommunityPo) {

        shopCommunityPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, 1);
        shopCommunityPo.setEndTime(DateUtil.getFormatTimeString(calendar.getTime(), DateUtil.DATE_FORMATE_STRING_A));
        shopCommunityPo.setState(ShopCommunityDto.STATE_W);
        shopCommunityPo.setMessage("入驻申请");
        shopCommunityPo.setScId(GenerateCodeFactory.getGeneratorId("11"));
        int flag = shopCommunityInnerServiceSMOImpl.saveShopCommunity(shopCommunityPo);
        if (flag > 0) {
            ShopGoodsPo shopGoodsPo = new ShopGoodsPo();
            shopGoodsPo.setCommunityId(shopCommunityPo.getCommunityId());
            shopGoodsPo.setCommunityName(shopCommunityPo.getCommunityName());
            shopGoodsPo.setState(shopCommunityPo.getState());
            shopGoodsPo.setEndTime(shopCommunityPo.getEndTime());
            shopGoodsPo.setStartTime(shopCommunityPo.getStartTime());
            shopGoodsPo.setMessage(shopCommunityPo.getMessage());
            shopGoodsPo.setScId(shopCommunityPo.getScId());
            shopGoodsPo.setShopId(shopCommunityPo.getShopId());
            shopGoodsPo.setStatusCd(shopCommunityPo.getStatusCd());
            shopGoodsInnerServiceSMOImpl.saveShopGoods(shopGoodsPo);//插入一份记录到冗余表
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
